<template>
  <div class="login-wrap">
    <div class="login-root">
      <div class="login-main">
        <div class="logo"><img src="../../assets/logo.png" height="90" width="450" /></div>
        <div class="login-container">
          <lay-form :model="loginForm" required>
            <lay-form-item :label-width="0">
              <lay-input placeholder="用户名" v-model="loginForm.Username" prefix-icon="layui-icon-user" size="lg">
              </lay-input>
            </lay-form-item>
            <lay-form-item :label-width="0">
              <lay-input placeholder="密码" passWord type="passWord" v-model="loginForm.Password"
                prefix-icon="layui-icon-password" size="lg"></lay-input>
            </lay-form-item>
            <span v-if="validCodeOn">
              <lay-form-item :label-width="0">
                <lay-col :md="24">
                  <lay-col :md="16">
                    <lay-input placeholder="验证码" type="validCode" v-model="loginForm.ValidCode"
                      prefix-icon="layui-icon-template-one" size="lg"></lay-input>
                  </lay-col>
                  <lay-col :md="8">
                    <span>
                      <img :src="codeUrl" @click="getCode" class="login-code-img" />
                    </span>
                  </lay-col>
                </lay-col>
              </lay-form-item>
            </span>
            <lay-form-item :label-width="0">
              <lay-checkbox name="like" v-model="loginForm.Remember" skin="primary" :value="true">记住密码</lay-checkbox>
            </lay-form-item>
            <lay-form-item :label-width="0" class="subbotton">
              <lay-button type="primary" @click.prevent="loginSubmit()" native-type="loginSubmit"
                prefix-icon="layui-icon-ok">登录</lay-button>
            </lay-form-item>
          </lay-form>
        </div>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { login, captcha, userInfo } from '../../api/module/user/user';
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { useUserStore } from '../../store/user';
import { useConfigStore } from '../../store/config';
import { layer } from "@layui/layer-vue";
import { setToken } from '../../library/cookieUtil'
import { useAppStore } from "../../store/app";
import Cookies from 'js-cookie'
import { Base64 } from 'js-base64'
const router = useRouter();
const userStore = useUserStore();
const configStore = useConfigStore();

let loginForm = reactive({
  Username: "",
  Password: "",
  Remember: true,
  ValidCode: '',
  Uuid: ''
})
const appStore = useAppStore();
const codeUrl = ref('')
// 验证码开关
const validCodeOn = ref(true)

const getCode = () => {
  captcha().then((res) => {
    codeUrl.value = 'data:image/gif;base64,' + res.data.img
    loginForm.Uuid = res.data.uuid
    validCodeOn.value = res.data.validCodeOn
  })
}
getCode()

let userName = Cookies.get('userName')
let passWord = Cookies.get('passWord')
loginForm.Username = userName === undefined ? loginForm.Username : Base64.decode(userName);
loginForm.Password = passWord === undefined ? loginForm.Password : Base64.decode(passWord);

const loginSubmit = async () => {
  if (!loginForm.Username || !loginForm.Password) {
    layer.msg("登录名或密码必填", { icon: 2 })
    return
  }
  try {
    let { data, status, msg } = await login(loginForm);
    if (status == 1) {
      layer.msg(msg, { icon: 1 }, async () => {
        if (loginForm.Remember == true) {
          setToken(data.token, 7);//7天持久化
          Cookies.set('userName', Base64.encode(loginForm.Username), { expires: 30 })
          Cookies.set('passWord', Base64.encode(loginForm.Password), { expires: 30 })
        } else {
          setToken(data.token, 1);//1天持久化
          Cookies.remove('userName')
          Cookies.remove('passWord')
        }
        await userInfo().then((res) => {
          if (res.status == 1) {
            userStore.userInfo = res.userInfo;
            userStore.statistics = res.statistics;
            userStore.userMenu = res.asMenus;
            userStore.menuBottons = res.asMenuBottons;
            configStore.systemConfig = res.sytemConfig;
            configStore.dicts = res.listViewDictData;
            configStore.parameter = res.configParms;
            appStore.modChange = "system";
            router.push('/');
          }
        });
      })
    } else {
      // 重新获取验证码
      if (validCodeOn.value) {
        getCode()
      }
      layer.msg(msg, { icon: 2 })
    }
  } catch (error) {
    getCode()
    console.log('er:', error)
  }

}

</script>

<style scoped>
.logo {
  margin: auto;
  text-align: center;
  height: 90px;
  width: 450px;
}

.login-code-img {
  width: 122px;
  height: 42px;
  margin-left: 10px;
  cursor: pointer;
}

.login-wrap {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  overflow: auto;
  z-index: 9;
  background-image: url("../../assets/login/loginbg.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  min-height: 100vh;
}

.login-wrap :deep(.layui-input-block) {
  margin-left: 0 !important;
}

.login-root {
  position: absolute;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  justify-content: center;
  width: 100%;
  min-width: 320px;
  background-color: initial;
}

.login-main {
  position: relative;
  display: block;
}

.login-container {
  padding: 30px;
  position: relative;
  overflow: hidden;
  width: 400px;
  max-width: calc(100vw - 28px);
  border: 1.5px solid #fff;
  border-radius: 8px;
  background: hsla(0, 0%, 100%, 0.5);
  backdrop-filter: blur(30px);
}

.login-container .layui-tab-head {
  background: transparent;
}

.login-container .layui-input-wrapper {
  margin-top: 10px;
  margin-bottom: 10px;
}

.login-container .layui-input-wrapper {
  margin-top: 12px;
  margin-bottom: 12px;
}

.login-container .assist {
  margin-top: 5px;
  margin-bottom: 5px;
  letter-spacing: 2px;
}

.login-container .layui-btn {
  margin: 10px 0px 10px 0px;
  letter-spacing: 2px;
  height: 40px;
}

.login-container .layui-line-horizontal {
  letter-spacing: 2px;
  margin-bottom: 34px;
  margin-top: 24px;
}

.subbotton {
  text-align: center;
}
</style>
